﻿package com.athena.process
{
	import flash.net.URLRequest ;
	import flash.events.*;
	import flash.display.*;
	/**
	 * ...
	 * @author ...
	 */
	public class BasicFunction 
	{
		
		public static function CompareArrCard(arr1 : Array, arr2 : Array) : int {
			arr1.sortOn(["NumberCard", "Suit"], [Array.NUMERIC, Array.NUMERIC]);
			arr2.sortOn(["NumberCard", "Suit"], [Array.NUMERIC, Array.NUMERIC]);
			var diem1, diem2 : int = 0 ;
			for (var i:int = 0; i < arr1.length; i++) 
			{
				if (arr1[i].NumberCard == 10) diem1 = diem1 +1 ;
				else diem1 = diem1 + arr1[i].NumberCard ;
				if (arr2[i].NumberCard == 10) diem2 = diem2 +1 ;
				else diem2 = diem2 + arr2[i].NumberCard ;
			}
			if (diem1 > diem2) {
				if (diem1 > 7) return 2 ;
				else return 1 ;
			}
			if (diem1 < diem2) {
				if (diem2 > 7) return -2 ;
				else return -1 ;
			}
			if (diem1 == diem2) {
				if (arr1[2].NumberCard + arr1[2].Suit * 10 > arr2[2].NumberCard + arr2[2].Suit * 10) {
					if (diem1 > 7) return 2
					else return 1 ;
				}
				else {
					if (diem2 > 7) return -2
					else return -1 ;
				}
			}
			return 0;
		}
		public static function convertTimeToString(number : Number) : String {
			//Cocaro.tf.appendText("=>Time:" + number) ;
			var now : Date = new Date() ;
			now.setTime(number) ;
			var strReturn : String = "" ;
			strReturn = now.getDate().toString() ;
			if (strReturn.length == 1) strReturn = "0" + strReturn ;
			var month : String = (now.getMonth() + 1).toString() ;
			if (month.length == 1) strReturn = strReturn + "/0" + month ;
			else strReturn = strReturn + "/" + month ;
			strReturn = strReturn + "/" + now.getFullYear().toString() ;
			strReturn = strReturn + " " + now.getHours().toString() ;
			strReturn = strReturn + ":" + now.getMinutes().toString() ;
			strReturn = strReturn + ":" + now.getSeconds().toString() ;
			return strReturn ;
		}
		/******************* Ham Chuan hoa So, Convert Number ==> String theo format *********************************/
		public static function numberFormat(number:Number, maxDecimals:int = 2, forceDecimals:Boolean = false, siStyle:Boolean = true):String {
			var i:int = 0, inc:Number = Math.pow(10, maxDecimals), str:String = String(Math.round(inc * Number(number))/inc);
			var hasSep:Boolean = str.indexOf(",") == -1, sep:int = hasSep ? str.length : str.indexOf(",");
			var ret:String = (hasSep && !forceDecimals ? "" : (siStyle ? "." : ",")) + str.substr(sep+1);
			if (forceDecimals) for (var j:int = 0; j <= maxDecimals - (str.length - (hasSep ? sep-1 : sep)); j++) ret += "0";
			while (i + 3 < (str.substr(0, 1) == "-" ? sep-1 : sep)) ret = (siStyle ? "," : ".") + str.substr(sep - (i += 3), 3) + ret;
			return str.substr(0, sep - i) + ret;
		}
		/****************** Ham Chuan hoa String **************************/
		public static function trim( s:String ):String	{
			//return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
		    //return s.replace(/^[\s|\t|\n]+|[\s|\t|\n]+$/gs,'');
			return s.replace(/^\s+|\s+$/g, "");
		}
		/****************** Ham Convert Card ******************************/
		public static function convertCard(m : int) : Object {
			for (var i:int = 0; i < Cocaro.ArrCard_Fix.length; i++) 
			{
				if (Cocaro.ArrCard_Fix[i].I == m) return Cocaro.ArrCard_Fix[i] ;
			}
			var obj : Object = new Object() ;
			obj.N = 0 ;
			obj.S = 0 ;
			return obj ;
		}
		/***************** Remove Icon Tren Giao dien ********************/
		public static function RemoveIcon() : void {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("IconFinished")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("IconFinished")) ;
			}
			mc = MovieClip(Cocaro.mainStage_mc.getChildByName("IconInGame")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("IconInGame")) ;
			}
		}
		/***************** Remove Popup Tren Giao dien ********************/
		public static function RemovePopup() : void {
			var mcPop : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Danhbiengioi")) ;
			while (mcPop != null) {
				Cocaro.mainStage_mc.removeChild(mcPop) ;
				mcPop = MovieClip(Cocaro.mainStage_mc.getChildByName("Danhbiengioi")) ;
			}
			mcPop = MovieClip(Cocaro.mainStage_mc.getChildByName("Danhcuoc")) ;
			while (mcPop != null) {
				Cocaro.mainStage_mc.removeChild(mcPop) ;
				mcPop = MovieClip(Cocaro.mainStage_mc.getChildByName("Danhcuoc")) ;
			}
			mcPop = MovieClip(Cocaro.mainStage_mc.getChildByName("Mark")) ;
			while (mcPop != null) {
				Cocaro.mainStage_mc.removeChild(mcPop) ;
				mcPop = MovieClip(Cocaro.mainStage_mc.getChildByName("Mark")) ;
			}
		}
		public static function RemoveHideLoading() : void {
			var tmpHide:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				Cocaro.mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));			
			}
			var tmpLD:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));			
			while (tmpLD != null) {
				Cocaro.mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));			
			}
		}		
		
		/***************** Show and Hide Popup Alert *****************************/
		public static function ShowAlert(str : String) : void {
			var tmpTB:MovieClip = MovieClip(new Popup_Alert());
			tmpTB.name = "Alert";
			tmpTB.x = 0;
			tmpTB.y = 0;
			tmpTB.Title_txt.text = "Thông báo" ;
			tmpTB.Content_txt.text = str ;
			tmpTB.Close_btn.addEventListener(MouseEvent.CLICK, Close_Click);						
			Cocaro.mainStage_mc.addChild(tmpTB);
		}
		public static function Close_Click(evt:MouseEvent) {
			Cocaro.SoundKick() ;
			var tmp :MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Alert")) ;
			while (tmp != null) {
				Cocaro.mainStage_mc.removeChild(tmp) ;
				tmp = MovieClip(Cocaro.mainStage_mc.getChildByName("Alert")) ;
			}
			RemoveHideLoading() ;
		}	
	}	
}
